<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div class="title_container">
        <img class="logo_sys" src="../assets/logo_sys.png" alt />
        <span class="title_sys">电商后台管理系统</span>
      </div>
      <div class="btns">
        <el-tooltip effect="dark" content="退出登录" :enterable="false" placement="top">
          <el-button
            size="medium"
            title="退出登录"
            id="exit_btn"
            type="danger"
            icon="el-icon-switch-button"
            circle
            @click="login_exit"
          ></el-button>
        </el-tooltip>
      </div>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 主题区域左边区域 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle_botton" @click="toggleCollapse">|||</div>
        <el-menu
          :unique-opened="true"
          background-color="#313743"
          text-color="#fff"
          active-text-color="rgb(53,155,255)"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="defaultActive"
        >
          <!-- 一级菜单 -->
          <el-submenu v-for="item in menulist" :key="item.id" :index="item.id +''">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- i是图标 iconClassFun(item.id)-->
              <i :class="iconsObj['id_' + item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="childrenItem in item.children"
              :key="childrenItem.id"
              :index="'/'+childrenItem.path"
              @click="setdefaultActive('/'+childrenItem.path)"
            >
              <template slot="title">
                <!-- i是图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{childrenItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主题区域右边区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data: function() {
    return {
      menulist: [],
      iconsObj: {
        id_125: "icon-yonghuguanli",
        id_103: "icon-quanxianshenpi",
        id_101: "icon-shangpinguanli3",
        id_102: "icon-dingdan",
        id_145: "icon-shujutongji3"
      },
      isCollapse: false, //默认不折叠
      defaultActive: ""
    };
  },
  created: function() {
    this.getMenuList();
    this.defaultActive = sessionStorage.getItem("defaultActive_session");
  },
  methods: {
    login_exit() {
      // 退出登录的时候提示信息
      this.$confirm("此操作将退出系统登录,是否退出?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        lockScroll: false
      }).then(() => {
        sessionStorage.removeItem("token"); //清除sessionStorage token
        sessionStorage.removeItem("defaultActive_session");
        this.$router.push("/login");
        sessionStorage.removeItem("defaultActive_session");
        this.$message({
          type: "success",
          message: "退出登录成功!"
        });
      });
    },
    async getMenuList() {
      // 获取数据库中的菜单内容
      const { data: res } = await this.$http.get("/menus");
      const resText = res.meta.msg;
      console.log(resText);

      if (res.meta.status != 200) {
        this.$message({
          type: "error",
          message: resText
        });
        if (res.meta.status == 400) {
          this.$router.push("/login");
        }
        return;
      } else {
        this.menulist = res.data;
      }
    },
    iconClassFun(id) {
      //显示不容的菜单图标
      const dataId = "id_" + id;
      for (let key in this.iconsObj) {
        if (dataId == key) {
          return this.iconsObj[key];
        }
      }
    },
    toggleCollapse() {
      // 显示和隐藏菜单
      this.isCollapse = !this.isCollapse;
    },
    setdefaultActive(val) {
      sessionStorage.setItem("defaultActive_session", val);
      this.defaultActive = val;
    }
  }
};
</script>
<style lang='less' scoped>
.el-container {
  height: 100%;
  width: 100%;
}
.el-header {
  position: relative;
  height: 55px !important;
  background-color: rgb(54, 61, 64);
  -moz-user-select: none; /*火狐*/

  -webkit-user-select: none; /*webkit浏览器*/

  -ms-user-select: none; /*IE10*/

  -khtml-user-select: none; /*早期浏览器*/

  user-select: none;
}
.el-aside {
  background-color: #313743;
  -moz-user-select: none; /*火狐*/

  -webkit-user-select: none; /*webkit浏览器*/

  -ms-user-select: none; /*IE10*/

  -khtml-user-select: none; /*早期浏览器*/

  user-select: none;
}
.el-main {
  background-color: #e9edf0;
}
.el-aside .el-menu {
  border-right: none;
}
.logo_sys {
  display: inline-block;
  height: 24px;
  width: 112px;
  vertical-align: middle;
  margin-top: -5px;
}
.title_sys {
  line-height: 55px;
  color: #fff;
  font-size: 24px;
  font-weight: bold;
  letter-spacing: 10px;
  margin-left: 20px;
}
.title_container {
  float: left;
}
.btns {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  justify-content: flex-end;
}
.toggle_botton {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #aba8a8;
  text-align: center;
  letter-spacing: 0.15em;
  cursor: pointer;
}
</style>